package org.reborn.utils

import java.util.Properties
import java.io.InputStream

object PropertyUtil {
  
  /**
   * Loads a .properties file and puts the results into a Map
   * @param propertiesFile the fully-qualified name of a properties file on the classpath (file extension is required)
   * @return Some[Map[propery,value]] if the file was found and read successfully, otherwise None
   */
  def ReadProperties(propertiesFile:String):Option[Map[String,String]] = {
	  
	var iStream:Option[InputStream] = None
	try{
	  iStream = FileUtil.LoadFromClasspath(propertiesFile)
	  
	  iStream.foreach{(stream) =>
	    var properties:Map[String,String] = Map()
	    
	 	val propFile = new Properties()
	 	propFile.load(stream)
	 	
	 	val itr = propFile.entrySet.iterator
	 	while(itr.hasNext){
	 	  val entry = itr.next
	 	  properties += (entry.getKey.asInstanceOf[String] -> entry.getValue.asInstanceOf[String])
	 	}
	    
	    return Some(properties)
	  }
	}
	finally{
	  iStream.foreach{ _.close }
	}

    System.err.println("Could not read "+propertiesFile)
	return None

  }

}